package com.qidian.buy.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2024/7/12 10:47
 */
public class DateTools {
    /**
     * @param pattern
     * @return
     */
    public static String getNowStr(String pattern) {
        String tempPattern = "yyyy-MM-dd HH:mm:ss";
        if (StringUtils.isNoneBlank(pattern)) {
            tempPattern = pattern;
        }
        synchronized (DateTools.class) {
            return new SimpleDateFormat(tempPattern).format(new Date());
        }
    }

    /**
     * @param pattern
     * @param src
     * @return
     */
    public static Date getDateFromString(String pattern, String src) {
        String tempPattern = "yyyy-MM-dd HH:mm:ss";
        if (StringUtils.isNoneBlank(pattern)) {
            tempPattern = pattern;
        }
        synchronized (DateTools.class) {
            try {
                return new SimpleDateFormat(tempPattern).parse(src);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * @param pattern
     * @param src
     * @return
     */
    public static String getDateFromString(String pattern, Date src) {
        String tempPattern = "yyyy-MM-dd HH:mm:ss";
        if (StringUtils.isNoneBlank(pattern)) {
            tempPattern = pattern;
        }
        synchronized (DateTools.class) {
            return new SimpleDateFormat(tempPattern).format(src);
        }
    }

}
